package com.xiaobias.com.xiaobias.order.web;

import com.xiaobias.com.xiaobias.order.pojo.Order;
import com.xiaobias.com.xiaobias.order.pojo.User;
import com.xiaobias.com.xiaobias.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping(value = {"order"})
public class OrderController {

    private final OrderService orderService;

    private final RestTemplate restTemplate;

    @Autowired
    public OrderController(OrderService orderService, RestTemplate restTemplate) {
        this.orderService = orderService;
        this.restTemplate = restTemplate;
    }

    @GetMapping(value = {"{orderId}"})
    public Order queryOrderById(@PathVariable(value = "orderId") Long orderId) {
        //1. 根据 id 查询订单
        Order order = this.orderService.queryOrderById(orderId);
        // 2. 利用RestTemplate 发起 http 请求，查询用户
        // 2.1 url
        String url = "http://userservice/user/" + order.getUserId();
        // 2 发起 http  请求
        User user = this.restTemplate.getForObject(url, User.class);
        // 3 封装 user 到 order
        order.setUser(user);
        // 4.返回
        return order;
    }
}
